'''
    把ip从1~255都ping一遍
    成功的地址就放入a.txt
    没成功的地址就放入b.txt
    如果运行出错捕获异常就输出”运行出错“
'''

import subprocess

a=[]
b=[]

i=1
try:
    while i<256:
        p=subprocess.call(f'ping -c 1 192.168.0.{i} ',shell=True)
        if p==0:
            print(f"192.168.0.{i} is ok")
            a.append(f"192.168.0.{i}")
        else:
            print(f"192.168.0.{i} is not ok")
            b.append(f"192.168.0.{i}")
        i+=1

    with open('a.txt','a') as fp:
        for j in a:
            fp.write(f'{j}\n')

    with open('b.txt', 'a') as fp:
        for j in b:
            fp.write(f'{j}\n')
except:
    print("运行出错")
